﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Group : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public string Command { get; set; }
        public string Description { get; set; }
        public string Id { get; set; }
        public string Image32by32Popup { get; set; }
        public string Image32by32PopupClass { get; set; }
        public int? Image32by32PopupLeft { get; set; }
        public int? Image32by32PopupTop { get; set; }
        public string PopupWidth { get; set; }
        public int? Sequence { get; set; }
        public string Template { get; set; }
        public string Title { get; set; }
        public Controls Controls { get; set; }

        public Group()
        {
            //TODO verif constructeur
            this.Controls = new Controls(true);
        }

        public Group(XmlNode node):this()
        {
            try
            {
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.Description = XmlHelper.GetAttribute(node, "Description");
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.Image32by32Popup = XmlHelper.GetAttribute(node, "Image32by32Popup");
                this.Image32by32PopupClass = XmlHelper.GetAttribute(node, "Image32by32PopupClass");
                this.Image32by32PopupLeft = XmlHelper.GetNullableIntAttribute(node, "Image32by32PopupLeft");
                this.Image32by32PopupTop = XmlHelper.GetNullableIntAttribute(node, "Image32by32PopupTop");
                this.PopupWidth = XmlHelper.GetAttribute(node, "PopupWidth");
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");
                this.Template = XmlHelper.GetAttribute(node, "Template");
                this.Title = XmlHelper.GetAttribute(node, "Title");

                if (node.SelectSingleNode("Controls") != null)
                {
                    this.Controls = new Controls(node.SelectSingleNode("Controls"), true);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode customActionNode)
        {
            try
            {
                XmlNode groupNode = XmlHelper.AddNode(customActionNode, "Group");

                XmlHelper.AddAttribute(groupNode, "Command", this.Command);
                XmlHelper.AddAttribute(groupNode, "Description", this.Description);
                XmlHelper.AddAttribute(groupNode, "Id", this.Id);
                XmlHelper.AddAttribute(groupNode, "Image32by32Popup", this.Image32by32Popup);
                XmlHelper.AddAttribute(groupNode, "Image32by32PopupClass", this.Image32by32PopupClass);
                XmlHelper.AddAttribute(groupNode, "Image32by32PopupLeft", this.Image32by32PopupLeft);
                XmlHelper.AddAttribute(groupNode, "Image32by32PopupTop", this.Image32by32PopupTop);
                XmlHelper.AddAttribute(groupNode, "PopupWidth", this.PopupWidth);
                XmlHelper.AddAttribute(groupNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(groupNode, "Template", this.Template);
                XmlHelper.AddAttribute(groupNode, "Title", this.Title);

                if (this.Controls != null)
                {
                    this.Controls.AddNode(groupNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Group");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Controls" }, true);

                if (this.Controls != null)
                {
                    this.Controls.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Controls))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    this.Controls = (Controls)child;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Controls)
                {
                    this.Controls = null;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
